package 递归.二分查找;

public class BinarySearchR {
    private BinarySearchR(){};
    private static  int search(int[] data, int target){
        return search(data, 0, data.length - 1, target);
    }
    private static int search(int[] data, int l, int r, int target){

      int mid = 0;
      while (l <= r){
          mid = (l+r) / 2;
          if (data[mid] == target){
              return mid;
          }else if(data[mid] < target){
              l = mid+1;
          }else {
              r = mid - 1;
          }
      }

      return -1;
    }

    public static void main(String[] args) {
        int[] a = {1,2,3,4,5,6};
        for (int i = 1; i < 8; i++) {
            System.out.println(BinarySearchR.search(a, i));
        }
    }
}
